
package utils;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * Allows to manipulate object and espacially read an object from a file
 * @author Equipe SimPack 2004
 * @created May 15, 2004
 */

public class ObjectWriter implements Serializable{
	
    // for log purposes
    private static final Logger fLogger = Logger.getLogger(ObjectReader.class.getPackage().getName());

    /**
     * Write an object in a file
     * @param ob object, in out case it will be a Simulator object
     * @param fileName filename of the file in which we'll write the object
     */
    public static void writeObject(Object ob, String fileName) {
	ObjectOutput output = null;
	try {
	    //use buffering
	    OutputStream file = new FileOutputStream(fileName);
	    OutputStream buffer = new BufferedOutputStream(file);
	    output = new ObjectOutputStream(buffer);
	    output.writeObject(ob);
	} 
	catch (IOException ex) {
	    fLogger.log(Level.SEVERE,"Cannot perform output (filename : " + fileName + ") .",ex);
	}
		
	finally{
	    try {
		if (output != null) {
		    //flush and close "output" and its underlying streams
		    output.close();
		}
	    }
	    catch (IOException ex ){
		fLogger.log(Level.SEVERE, "Cannot close output stream.", ex);
	    }
	}
    }

    public static void main(String[] args) {
    }
}
